package com.cskaoyan.service.goods;

import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.goods.*;
import com.cskaoyan.bean.goods.base.GoodsPageData;
import com.cskaoyan.bean.goods.bo.GoodsBo;
import com.cskaoyan.bean.goods.bo.GoodsSearchBO;
import com.cskaoyan.bean.goods.vo.BrandVo;
import com.cskaoyan.bean.goods.vo.CategoryBrandAndBrandInfoVo;
import com.cskaoyan.bean.goods.vo.CategoryVo;
import com.cskaoyan.bean.goods.vo.GoodsDetailVo;
import com.cskaoyan.bean.mall.category.Category;
import com.cskaoyan.mapper.goods.GoodsAttributeMapper;
import com.cskaoyan.mapper.goods.GoodsMapper;
import com.cskaoyan.mapper.goods.GoodsProductMapper;
import com.cskaoyan.mapper.goods.GoodsSpecificationMapper;
import com.cskaoyan.mapper.mall.CategoryMapper;
import com.cskaoyan.utils.Constant;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description:商品模块接口的实现类(最tm难写的地方,🐕🐶)
 * @author: Jason 805636901@qq.com
 * @create: 2021-10-19-2:49 PM
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    GoodsAttributeMapper goodsAttributeMapper;
    @Autowired
    GoodsSpecificationMapper goodsSpecificationMapper;
    @Autowired
    GoodsProductMapper goodsProductMapper;
    @Autowired
    CategoryMapper categoryMapper;


    @Override
    public GoodsPageData queryGoods(BaseParam baseParam, GoodsSearchBO goodsSearchBO) {
        GoodsExample example = new GoodsExample();

        if (baseParam.getPage() != null) {
            //配置分页信息
            PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
            example.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder()); //这里一定要有空格否则语法错误,语句拼接不起来
        }

        GoodsExample.Criteria criteria = example.createCriteria();

        //查询条件
        if (goodsSearchBO.getName() != null && !"".equals(goodsSearchBO.getName())) {
            criteria.andNameLike("%" + goodsSearchBO.getName() + "%");
        }
        if (goodsSearchBO.getGoodsId() != null && !"".equals(goodsSearchBO.getGoodsId())) {
            criteria.andIdEqualTo(goodsSearchBO.getGoodsId());
        }
        if (goodsSearchBO.getGoodsSn() != null && !"".equals(goodsSearchBO.getGoodsSn())) {
            criteria.andGoodsSnEqualTo(goodsSearchBO.getGoodsSn());
        }

        criteria.andDeletedEqualTo(false); //排除掉已经逻辑删除掉的商品.

        //设置排序
        List<Goods> goods = goodsMapper.selectByExampleWithBLOBs(example);
        return GoodsPageData.ok(goods);
    }

    @Override
    public CategoryBrandAndBrandInfoVo getCategoryAndBrand() {
        List<BrandVo> brandVos = goodsMapper.selectBrandList();
        List<CategoryVo> categoryVos = goodsMapper.selectCategoryList();
        CategoryBrandAndBrandInfoVo categoryBrandAndBrandInfoVo = new CategoryBrandAndBrandInfoVo();//俄罗斯套娃的诞生
        categoryBrandAndBrandInfoVo.setBrandList(brandVos);//我开始套了
        categoryBrandAndBrandInfoVo.setCategoryList(categoryVos);
        return categoryBrandAndBrandInfoVo; //返回俄罗斯套娃
    }

    @Transactional //增加事务,变成原子操作.
    @Override
    public int deleteGoods(Goods goods) {
        //deleted false 为未删除 ; 1 为逻辑删除.
        //更新商品表
        goods.setDeleted(true);
        goodsMapper.updateByPrimaryKeySelective(goods);

        //更新属性表
        GoodsAttribute attribute = new GoodsAttribute();
        attribute.setGoodsId(goods.getId());//获取要更新的id
        attribute.setDeleted(true);
        GoodsAttributeExample attributeExample = new GoodsAttributeExample();
        attributeExample.createCriteria().andGoodsIdEqualTo(goods.getId());
        goodsAttributeMapper.updateByExampleSelective(attribute, attributeExample);

        //规模表
        GoodsSpecification specification = new GoodsSpecification();
        specification.setGoodsId(goods.getId());
        specification.setDeleted(true);
        GoodsSpecificationExample specificationExample = new GoodsSpecificationExample();
        specificationExample.createCriteria().andGoodsIdEqualTo(goods.getId());
        goodsSpecificationMapper.updateByExampleSelective(specification, specificationExample);

        //产品表
        GoodsProduct product = new GoodsProduct();
        product.setGoodsId(goods.getId());
        product.setDeleted(true);
        GoodsProductExample productExample = new GoodsProductExample();
        productExample.createCriteria().andGoodsIdEqualTo(goods.getId());
        goodsProductMapper.updateByExampleSelective(product, productExample);
        return Constant.SUCCESS;
    }

    @Transactional //增加事务,因为涉及多张表的更新变成原子操作.
    @Override
    // TODO: 还没在BO哪儿校验参数
    public int updateGoods(GoodsBo goodsInfo) {
        Goods goods = goodsInfo.getGoods();
        List<GoodsAttribute> attributes = goodsInfo.getAttributes();
        List<GoodsProduct> products = goodsInfo.getProducts();
        List<GoodsSpecification> specifications = goodsInfo.getSpecifications();

        //更新商品表
        goods.setUpdateTime(new Date());
        goodsMapper.updateByPrimaryKeySelective(goods);

        //属性表
        for (GoodsAttribute attribute : attributes) {
            attribute.setUpdateTime(new Date());
            goodsAttributeMapper.updateByPrimaryKeySelective(attribute);
        }

        //产品表
        for (GoodsProduct product : products) {
            product.setUpdateTime(new Date());
            goodsProductMapper.updateByPrimaryKeySelective(product);
        }

        //规模表
        for (GoodsSpecification specification : specifications) {
            specification.setUpdateTime(new Date());
            goodsSpecificationMapper.updateByPrimaryKeySelective(specification);
        }

        return Constant.SUCCESS;
    }

    @Override
    public int createGoods(GoodsBo goodsInfo) {
        Goods goods = goodsInfo.getGoods();
        List<GoodsAttribute> attributes = goodsInfo.getAttributes();
        List<GoodsProduct> products = goodsInfo.getProducts();
        List<GoodsSpecification> specifications = goodsInfo.getSpecifications();

        //商品表
        goods.setAddTime(new Date());
        goods.setUpdateTime(new Date());
        goodsMapper.insertSelective(goods);

        //属性表
        for (GoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goods.getId());
            attribute.setAddTime(new Date());
            attribute.setUpdateTime(new Date());
            goodsAttributeMapper.insertSelective(attribute);
        }

        //产品表
        for (GoodsProduct product : products) {
            product.setGoodsId(goods.getId());
            product.setAddTime(new Date());
            product.setUpdateTime(new Date());
            goodsProductMapper.insertSelective(product);
        }

        //规模表
        for (GoodsSpecification specification : specifications) {
            specification.setGoodsId(goods.getId());
            specification.setAddTime(new Date());
            specification.setUpdateTime(new Date());
            goodsSpecificationMapper.insertSelective(specification);
        }
        return Constant.SUCCESS;
    }

    @Override
    public GoodsDetailVo getGoodsDetail(Integer id) {
        GoodsDetailVo goodsDetailVo = new GoodsDetailVo();
        //获取goods信息
        Goods goods = goodsMapper.selectByPrimaryKey(id);
        goodsDetailVo.setGoods(goods);
        //获取品类id
        Category category = categoryMapper.selectByPrimaryKey(goods.getCategoryId());
        List<Integer> categoryList = new ArrayList<>();
        categoryList.add(category.getPid());
        categoryList.add(category.getId());
        goodsDetailVo.setCategoryIds(categoryList);

        //获取属性列表
        GoodsAttributeExample goodsAttributeExample = new GoodsAttributeExample();
        GoodsAttributeExample.Criteria criteria = goodsAttributeExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andGoodsIdEqualTo(id);
        List<GoodsAttribute> goodsAttributes = goodsAttributeMapper.selectByExample(goodsAttributeExample);
        goodsDetailVo.setAttributes(goodsAttributes);

        //获取规格列表
        GoodsSpecificationExample goodsSpecificationExample = new GoodsSpecificationExample();
        GoodsSpecificationExample.Criteria criteria1 = goodsSpecificationExample.createCriteria();
        criteria1.andGoodsIdEqualTo(id);
        criteria1.andDeletedEqualTo(false);
        List<GoodsSpecification> goodsSpecifications = goodsSpecificationMapper.selectByExample(goodsSpecificationExample);
        goodsDetailVo.setSpecifications(goodsSpecifications);


        //获取产品列表
        GoodsProductExample goodsProductExample = new GoodsProductExample();
        GoodsProductExample.Criteria criteria2 = goodsProductExample.createCriteria();
        criteria2.andGoodsIdEqualTo(id);
        criteria2.andDeletedEqualTo(false);
        List<GoodsProduct> goodsProducts = goodsProductMapper.selectByExample(goodsProductExample);
        goodsDetailVo.setProducts(goodsProducts);
        return goodsDetailVo;


    }


}

